package com.service.gmall.web.controller;

import com.service.gmall.common.result.Result;
import com.service.gmall.feign.item.SkuDetailsFeignClient;
import com.service.gmall.model.to.SkuDetailsTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author alpha
 * @className: ItemController
 * @date 2022/8/26 14:52
 * @Description
 */


@Controller
public class ItemController {
    @Autowired
    SkuDetailsFeignClient skuDetailsFeignClient;
    /**
     * @param skuId
     * @description: 商品详情页
     * @return: com.service.gmall.common.result.Result
     * @author: alpha
     * @date: 2022/8/26 19:06
     */
    @GetMapping("/{skuId}.html")
    public String getSkuDetails(@PathVariable("skuId") Long skuId, Model model){
        //远程调用service-item服务查询
        Result<SkuDetailsTo> result = skuDetailsFeignClient.getDetails(skuId);
        if (result.isOk()){
            SkuDetailsTo skuDetailsTo = result.getData();

            if(skuDetailsTo == null || skuDetailsTo.getSkuInfo() == null){
                //说明远程没有查到商品
                return "item/404";
            }
            model.addAttribute("categoryView",skuDetailsTo.getCategoryView());
            model.addAttribute("skuInfo",skuDetailsTo.getSkuInfo());
            model.addAttribute("spuSaleAttrList",skuDetailsTo.getSpuSaleAttrList());
            model.addAttribute("valuesSkuJson",skuDetailsTo.getValuesSkuJson());
            model.addAttribute("price",skuDetailsTo.getPrice());
        }

        return "item/index";
    }
}
